package com.niubility.controller;

import com.niubility.entity.Article;
import com.niubility.entity.Department;
import com.niubility.entity.SearchMain;

import com.niubility.entity.Users;
import com.niubility.exception.CustomException;
import com.niubility.server.Impl.ArticleServer;
import com.niubility.server.Impl.DepartmentService;
import com.niubility.server.Impl.SearchServer;
import com.niubility.server.Impl.UsersService;

import com.niubility.vo.ArticleAll;
import com.niubility.vo.SearchVo;
import net.sf.json.JSONArray;

import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
public class SearchController {

    @Resource
    private SearchServer searchServer;

    @Resource
    private UsersService usersService;

    @Resource
    private ArticleServer articleServer;

    @Resource
    private DepartmentService departmentService;

    Logger logger = Logger.getLogger(SearchController.class);
    //在此添加三张表的业务层接口

    /*
    * @Author:John
    * @Description:进行全局搜索
    * @Date: John 2018/2/1
    * @Params:
    * @Return:
    */
    @RequestMapping("/SearchOverAll")
    public void SeachOverAll(HttpServletRequest request, HttpServletResponse response) throws Exception {
        response.setCharacterEncoding("utf-8");
        String callback = request.getParameter("callback");
        String mainText = request.getParameter("mainText");
        SearchMain searchMain = new SearchMain();
        searchMain.setMaix(mainText);
        List<Integer> list = null;
        Object back = null;
        String result = null;
        List<SearchVo> list2 = new ArrayList<SearchVo>();
        try {

            list = searchServer.Select(searchMain);


            ////System.out.println(list.size());
            for (int i = 0;i<list.size();i++){
                SearchVo vo = new SearchVo();

                if(list.get(i)>99999999 && list.get(i)<199999999){
                    vo.setId(list.get(i));
                    Users users = usersService.LoadById(list.get(i));
                    if(users == null) continue;
                    vo.setMain(usersService.LoadById(list.get(i)).getUserSign());
                }
                if(list.get(i)>199999999 && list.get(i)<299999999){
                    vo.setId(list.get(i));
                    Department department = departmentService.loadDepById(list.get(i));
                    if(department == null) continue;
                    vo.setMain(departmentService.loadDepById(list.get(i)).getDepSign());
                }
                if(list.get(i)>299999999 && list.get(i)<399999999){
                    vo.setId(list.get(i));
                    ArticleAll article = articleServer.SelectById(list.get(i));
                    if(article == null) continue;
                    vo.setMain(article.getArtTexthead());
                }

                list2.add(vo);
            }
            ////System.out.println(list2);
            result = callback+"("+ JSONArray.fromObject(list2).toString()+")";
            ////System.out.println(result);
        }catch (Exception e){
            logger.debug("传输错误"+e);
            result = callback+"({"+"\"returncode\":"+108+"})";
            try {
                response.getWriter().write(result);
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            throw new CustomException();
        }
        try {
            response.getWriter().write(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @RequestMapping("/SearchFor")
    public void SearchFor(HttpServletResponse response,HttpServletRequest request) throws CustomException {
        response.setCharacterEncoding("utf-8");
        int id = Integer.parseInt(request.getParameter("id"));
        String callback = request.getParameter("callback");
        ////System.out.println(id+"-"+callback);
        String result = "";
        try {
            if(id>99999999 && id<199999999){
                Users users = usersService.LoadById(id);
                result = "("+ JSONObject.fromObject(users)+")";
            }
            if(id>199999999 && id<299999999){
                Department department = departmentService.loadDepById(id);
                result = "("+ JSONObject.fromObject(department)+")";
            }
            if(id>299999999 && id<399999999){
                ArticleAll articleAll = articleServer.SelectById(id);
                result = "("+ JSONObject.fromObject(articleAll).toString()+")";
            }
            ////System.out.println(result);
        }catch (Exception e){
            logger.debug("获取数据失败");
            result = callback+"({"+"\"returncode\":"+108+"})";
            try {
                response.getWriter().write(result);
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            throw new CustomException();
        }

        try {
            response.getWriter().write(callback+result);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
